/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * 判断是否是大驼峰命名
 * @param {String} word
 * @returns {boolean}
 */
const checkBigHump = (word) => {
  const re = /[a-zA-Z]/;
  const curWord = word
    .split("")
    .filter((x) => re.test(x))
    .join("");
  if (word.length === 1 && /^[A-Z]$/.test(word)) {
    return true;
  }
  const words = curWord.split(/(?=[A-Z])/);
  for (let i = 0; i < words.length; i++) {
    if (!/^[A-Z][a-z]+$/.test(words[i])) {
      return false;
    }
  }
  return true;
};

/**
 * 判断是否是小驼峰命名
 * @param {String} word
 * @returns {boolean}
 */
const checkSmallHump = (word = "") => {
  if (!/[a-z]/.test(word[0])) {
    return false;
  }
  let index = -1;
  for (let i = 0; i < word.length; i++) {
    if (/[A-Z]/.test(word[i])) {
      index = i;
      break;
    }
  }
  return index === -1 || checkBigHump(word.slice(index, word.length));
};

// 判断是否是常量
const isAllWordUpper = (word = "") => {
  if (!word) {
    return false;
  }
  for (let i = 0; i < word.length; i++) {
    const flag = (word[i] >= "A" && word[i] <= "Z") || word[i] === "_";
    if (!flag) {
      return false;
    }
  }
  return true;
};

export { checkBigHump, checkSmallHump, isAllWordUpper };
